package com.hairline.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hairline.bean.Admin;
import com.hairline.bean.AdminExample;
import com.hairline.bean.RoleExample;
import com.hairline.bean.bo.AdminCreateBo;
import com.hairline.bean.bo.AdminDeleteBo;
import com.hairline.bean.bo.AdminUpdateBo;
import com.hairline.bean.vo.AdminCreateVo;
import com.hairline.bean.vo.AdminListVofl;
import com.hairline.bean.vo.AdminUpdateVo;
import com.hairline.bean.vo.RoleOptionsVo;
import com.hairline.mapper.AdminMapper;
import com.hairline.mapper.RoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by fenglin on 2020/9/1 10:45 PM
 */
@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    RoleMapper roleMapper;
    @Autowired
    AdminMapper adminMapper;

    ///admin/admin/list?page=1&limit=20&sort=add_time&order=desc
    @Override
    public Map<String, Object> adminList(Integer page, Integer limit, String sort, String order, String username) {
        //分页
        PageHelper.startPage(page, limit);
        Map<String, Object> map = new HashMap<>();
        AdminExample adminExample = new AdminExample();
        //排序方式
        adminExample.setOrderByClause(sort + " " + order);
        AdminExample.Criteria criteria = adminExample.createCriteria();
        if(username != null){
            criteria.andUsernameLike("%" + username + "%");
        }

        List<AdminListVofl> adminListVoList = adminMapper.selectAdminListByExample(adminExample);

        //使用PageHelper插件中PageInfo类
        PageInfo<AdminListVofl> info = new PageInfo<>(adminListVoList);

        map.put("total", info.getTotal());
        map.put("items", adminListVoList);

        return map;
    }

//    @Override
//    public List<RoleOptionsVo> roleOptions() {
//        RoleExample roleExample = new RoleExample();
//        List<RoleOptionsVo> list = roleMapper.selectRoleOptionsByExample(roleExample);
//
//        return list;
//    }

    @Override
    public AdminCreateVo adminCreate(AdminCreateBo adminCreateBo) {
        //接收数据{"username":"q","password":"","avatar":"i6.png","roleIds":[1,2]}
        //用户重复，602
        List<Admin> adminList = adminMapper.selectByExample(null);
        String username = adminCreateBo.getUsername();

        for(Admin i: adminList){
            if(username.equals(i.getUsername())){
                return null;
            }
        }
        //否则 创建
        Admin admin = new Admin();
        admin.setUsername(adminCreateBo.getUsername());
        admin.setPassword(adminCreateBo.getPassword());
        admin.setAvatar(adminCreateBo.getAvatar());
        admin.setRoleIds(adminCreateBo.getRoleIds());

        Date date = new Date();
        admin.setAddTime(date);
        admin.setUpdateTime(date);
        //TODO
        int i = adminMapper.insertSelective(admin);
        int id = adminMapper.selectIdLastInsertByUsername(admin);

        AdminCreateVo adminCreateVo = new AdminCreateVo(id,admin.getUsername(),
                admin.getPassword(), admin.getAddTime(), admin.getUpdateTime(),admin.getRoleIds());

        return adminCreateVo;
    }

    @Override
    public AdminUpdateVo adminUpdate(AdminUpdateBo adminUpdateBo) {
        Admin admin = new Admin(adminUpdateBo.getId(),adminUpdateBo.getUsername(),adminUpdateBo.getAvatar(),
                adminUpdateBo.getRoleIds(), adminUpdateBo.getPassword());
        //TODO
        int i = adminMapper.updateByPrimaryKey(admin);
        AdminUpdateVo adminUpdateVo = new AdminUpdateVo(admin.getId(),admin.getUsername(),admin.getAvatar(),
                admin.getRoleIds(),admin.getPassword(),admin.getUpdateTime());
        return adminUpdateVo;
    }

    @Override
    public int adminDelete(AdminDeleteBo adminDeleteBo) {
        int id = adminDeleteBo.getId();
        return adminMapper.deleteByPrimaryKey(id);
    }


}
